﻿namespace MSRI.SmsService
{
    using MSRI.HelperClasses;
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;

    public class SmsPublishClass : MarshalByRefObject
    {
        public static List<ClientClass> _ClientList = new List<ClientClass>();
        public static bool _IsConnected = false;
        private static string _ServerName = null;

        public event OnConnectedDelegate OnClientConnected;

        public event OnConnectedDelegate OnClientDisconnected;

        public event OnSendMessageDelegate OnClientSendMessage;

        public event OnSendSmsDelegate OnClientSendSms;

        public void Client2Server_Connected(string ClientName)
        {
            if ((this.OnClientConnected != null) && (this.OnClientConnected.GetInvocationList().Length > 0))
            {
                this.OnClientConnected(ClientName);
            }
        }

        public void Client2Server_Disconnected(string ClientName)
        {
            if ((this.OnClientDisconnected != null) && (this.OnClientDisconnected.GetInvocationList().Length > 0))
            {
                this.OnClientDisconnected(ClientName);
            }
        }

        public void Client2Server_SendMessage(string ClientName, string Message)
        {
            if ((this.OnClientSendMessage != null) && (this.OnClientSendMessage.GetInvocationList().Length > 0))
            {
                this.OnClientSendMessage(ClientName, Message);
            }
        }

        public void Client2Server_SendSms(string ClientName, SmsClass Message)
        {
            if ((this.OnClientSendSms != null) && (this.OnClientSendSms.GetInvocationList().Length > 0))
            {
                this.OnClientSendSms(ClientName, Message);
            }
        }

        public bool DisposeClient(string ClientName)
        {
            if ((ClientName != null) && (ClientName.Trim().Length != 0))
            {
                for (int i = 0; i < _ClientList.Count; i++)
                {
                    if (_ClientList[i].ClientName == ClientName)
                    {
                        _ClientList[i].DeliveryReportDelegate = null;
                        _ClientList[i].MessageReceivedDelegate = null;
                        _ClientList[i].SmsReceivedDelegate = null;
                        _ClientList.RemoveAt(i);
                        return true;
                    }
                }
            }
            return false;
        }

        public bool IsClientConnected()
        {
            bool flag = true;
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                if (class2.IsClientConnected != null)
                {
                    try
                    {
                        class2.IsClientConnected();
                    }
                    catch
                    {
                        this.DisposeClient(class2.ClientName);
                        i--;
                        flag = false;
                    }
                }
            }
            return flag;
        }

        public bool RegisterClient(string ClientName)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                if (class2.ClientName == ClientName)
                {
                    return false;
                }
            }
            ClientClass item = new ClientClass();
            item.ClientName = ClientName;
            _ClientList.Add(item);
            return true;
        }

        public bool RegisterClient(string __ClientName, OnMessageReceivedDelegate __HostToClientMessage, OnSmsReceivedDelegate __HostToClientSms, OnDeliveryReportDelegate __HostToClientDeliveryReport, IsClientConnectedDelegate __HostToClientIsConnected)
        {
            ClientClass item = null;
            for (int i = 0; i < _ClientList.Count; i++)
            {
                item = _ClientList[i];
                if (item.ClientName == __ClientName)
                {
                    try
                    {
                        item.IsClientConnected();
                        return false;
                    }
                    catch
                    {
                        this.DisposeClient(item.ClientName);
                        i--;
                    }
                }
            }
            item = new ClientClass(__ClientName, __HostToClientMessage, __HostToClientSms, __HostToClientDeliveryReport, __HostToClientIsConnected);
            _ClientList.Add(item);
            return true;
        }

        public bool RegisterDeliveryReportDelegate(string ClientName, OnDeliveryReportDelegate del)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                try
                {
                    if (class2.ClientName == ClientName)
                    {
                        class2.DeliveryReportDelegate = del;
                        return true;
                    }
                }
                catch
                {
                }
            }
            return false;
        }

        public bool RegisterMessageDelegate(string ClientName, OnMessageReceivedDelegate del)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                try
                {
                    if (class2.ClientName == ClientName)
                    {
                        class2.MessageReceivedDelegate = del;
                        return true;
                    }
                }
                catch
                {
                }
            }
            return false;
        }

        public bool RegisterSmsDelegate(string ClientName, OnSmsReceivedDelegate del)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                if (class2.ClientName == ClientName)
                {
                    class2.SmsReceivedDelegate = del;
                    return true;
                }
            }
            return false;
        }

        public void Server2Client_DeliveryReportReceived(string ClientName, DeliveryReportClass Report, bool IncludeClient)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                try
                {
                    if (IncludeClient)
                    {
                        if (((class2.ClientName == ClientName) || (ClientName == "*")) && (class2.DeliveryReportDelegate != null))
                        {
                            class2.DeliveryReportDelegate(new DeliveryReportClass(Report));
                        }
                    }
                    else if ((class2.ClientName != ClientName) && (class2.DeliveryReportDelegate != null))
                    {
                        class2.DeliveryReportDelegate(new DeliveryReportClass(Report));
                    }
                }
                catch
                {
                }
            }
        }

        public void Server2Client_MessageReceived(string ClientName, string Message, bool IncludeClient)
        {
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class2 = _ClientList[i];
                try
                {
                    if (IncludeClient)
                    {
                        if (((class2.ClientName == ClientName) || (ClientName == "*")) && (class2.MessageReceivedDelegate != null))
                        {
                            class2.MessageReceivedDelegate(Message);
                        }
                    }
                    else if ((class2.ClientName != ClientName) && (class2.MessageReceivedDelegate != null))
                    {
                        class2.MessageReceivedDelegate(Message);
                    }
                }
                catch
                {
                }
            }
        }

        public SmsResponseClass Server2Client_SmsReceived(string ClientName, SmsClass Message, bool IncludeClient)
        {
            SmsResponseClass class2 = new SmsResponseClass();
            for (int i = 0; i < _ClientList.Count; i++)
            {
                ClientClass class3 = _ClientList[i];
                try
                {
                    if (IncludeClient)
                    {
                        if (((class3.ClientName == ClientName) || (ClientName == "*")) && (class3.SmsReceivedDelegate != null))
                        {
                            SmsResponseClass class4 = class3.SmsReceivedDelegate(new SmsClass(Message));
                            if (class4 != null)
                            {
                                for (int j = 0; j < class4.ResponseMessages.Count; j++)
                                {
                                    class4.ResponseMessages[j].ClientName = class3.ClientName;
                                    class2.AddResponseSms(class4.ResponseMessages[j]);
                                }
                            }
                        }
                    }
                    else if ((class3.ClientName != ClientName) && (class3.SmsReceivedDelegate != null))
                    {
                        SmsResponseClass class5 = class3.SmsReceivedDelegate(new SmsClass(Message));
                        if (class5 != null)
                        {
                            for (int k = 0; k < class5.ResponseMessages.Count; k++)
                            {
                                class5.ResponseMessages[k].ClientName = class3.ClientName;
                                class2.AddResponseSms(class5.ResponseMessages[k]);
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            return class2;
        }

        public bool IsConnected
        {
            get
            {
                return _IsConnected;
            }
            set
            {
                _IsConnected = value;
            }
        }

        public string ServerName
        {
            get
            {
                return _ServerName;
            }
        }

        public class ClientClass
        {
            private string _ClientName;
            private OnDeliveryReportDelegate _ServerToClientDeliveryReport;
            private IsClientConnectedDelegate _ServerToClientIsConnected;
            private OnMessageReceivedDelegate _ServerToClientMessage;
            private OnSmsReceivedDelegate _ServerToClientSms;

            public ClientClass()
            {
                this._ClientName = "";
            }

            public ClientClass(string __ClientName, OnMessageReceivedDelegate __HostToClientMessage, OnSmsReceivedDelegate __HostToClientSms, OnDeliveryReportDelegate __HostToClientDeliveryReport, IsClientConnectedDelegate __HostToClientIsConnected)
            {
                this._ClientName = "";
                this._ClientName = __ClientName;
                this._ServerToClientMessage = __HostToClientMessage;
                this._ServerToClientDeliveryReport = __HostToClientDeliveryReport;
                this._ServerToClientSms = __HostToClientSms;
                this._ServerToClientIsConnected = __HostToClientIsConnected;
            }

            public string ClientName
            {
                get
                {
                    return this._ClientName;
                }
                set
                {
                    this._ClientName = value;
                }
            }

            public OnDeliveryReportDelegate DeliveryReportDelegate
            {
                get
                {
                    return this._ServerToClientDeliveryReport;
                }
                set
                {
                    this._ServerToClientDeliveryReport = value;
                }
            }

            public IsClientConnectedDelegate IsClientConnected
            {
                get
                {
                    return this._ServerToClientIsConnected;
                }
                set
                {
                    this._ServerToClientIsConnected = value;
                }
            }

            public OnMessageReceivedDelegate MessageReceivedDelegate
            {
                get
                {
                    return this._ServerToClientMessage;
                }
                set
                {
                    this._ServerToClientMessage = value;
                }
            }

            public OnSmsReceivedDelegate SmsReceivedDelegate
            {
                get
                {
                    return this._ServerToClientSms;
                }
                set
                {
                    this._ServerToClientSms = value;
                }
            }
        }
    }
}

